package com.sunni.controller;


import com.sunni.entity.Adress;
import com.sunni.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/view")
//@SessionAttributes(value = {"user","address"})
//@SessionAttributes(types = {User.class,Adress.class})
public class View {

    /**
     * 直接传递
     * @param map --> 前台
     * @return View视图 jsp页面
     */
    @RequestMapping("/map")
    public String getMap(Map<Object,Object> map){
        User user = new User(1,"五五开",null);
        map.put("user",user);
        return "view";
    }

    /**
     * ModelAndView  传递数据、访问视图
     * @return ModelAndView（数据 + 视图）
     */
    @RequestMapping("/modelAndView1")
    public ModelAndView modelAndView1(){
        User user = new User(1,"五五开",null);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("view");
        return modelAndView;
    }

    @RequestMapping("/modelAndView2")
    public ModelAndView modelAndView2(){
        User user = new User(1,"五五开",null);
        ModelAndView modelAndView = new ModelAndView("view");
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    /**
     * 使用 Request原生 传递参数
     * @param request 原生Request传递
     * @return 视图jsp
     */
    @RequestMapping("/request")
    public String getRequest(HttpServletRequest request){
        User user = new User(1,"五五开",null);
        request.setAttribute("user",user);
        return "view";
    }

    /**
     * 1. @ModelAttribute 把所有数据放到里面，，，，所有方法执行前，都会先访问这个方法。
     * 2. @RequestMapping("/modAttr")访问视图。。。（会拿到@ModelAttribute里的数据）
     * @param model
     */
    @ModelAttribute
    public void modAttr(Model model){
        User user = new User(1,"五五开",null);
        model.addAttribute("user",user);
    }
    @RequestMapping("/modAttr")
    public String modAttr_View(){
        return "view";
    }

    /**
     * 往Session里传入数据
     * @param session 获取Session
     * @return 视图jsp
     */
    @RequestMapping("/session")
    public String getSession(HttpSession session){
        User user = new User(1,"五五开",null);
        session.setAttribute("user",user);
        return "view";
    }

    /**
     * application 无法通过SpringMVC自动获取。（SpringMVC调用application无参构造器init, 但是application没有）。
     * 所以，通过request间接获取application。
     * @param request 通过request，获取 application。
     * @return 视图jsp
     */
    @RequestMapping("/application")
    public String getSession(HttpServletRequest request){
        User user = new User(1,"五五开",null);
        ServletContext application = request.getServletContext();
        application.setAttribute("user",user);
        return "view";
    }
//  错误写法
/*
    @RequestMapping("/application")
    public String getSession(ServletContext application){
        User user = new User(1,"五五开",null);
        application.setAttribute("user",user);
        return "view";
    }
*/


}
